package util;

import data.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;

import org.jdom.Document;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

public class AtomPublisher {

	public static void publishFeed(String searchString, ArrayList<RecipeData> recipes){
		//Use JDOM to create the atom specification and then post to the eXist database.
		String link = "http://localhost:8080/exist/atom/edit/FoodAndWine/" + searchString;
		ArrayList<Document> doms = XMLConverter.convertToXML(searchString.replace("_"," "), recipes, link);
		for(Document dom : doms){
			try {
				postToEXist(link, dom);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static void initializeContainer(){
		SAXBuilder builder = new SAXBuilder();
		Document dom = null;
		try {
			dom = builder.build("src/Init.xml");
			postToEXist("http://localhost:8080/exist/atom/edit/FoodAndWine/", dom);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	public static void deleteFromEXist(String urlStr) throws Exception{
		URL url = new URL(urlStr);
		HttpURLConnection conn =
			(HttpURLConnection) url.openConnection();
		conn.setRequestMethod("DELETE");
		conn.setDoOutput(true);
		conn.setDoInput(true);
		conn.setUseCaches(false);
		conn.setAllowUserInteraction(false);
		conn.setRequestProperty("Content-Type",
		"application/atom+xml");

		if (conn.getResponseCode() != 200) {
			throw new IOException(conn.getResponseMessage());
		}

	}
	
	public static Document getFromEXist(String urlStr) throws Exception{
		URL url = new URL(urlStr);
		HttpURLConnection conn =
			(HttpURLConnection) url.openConnection();
		conn.setRequestMethod("GET");
		conn.setDoOutput(true);
		conn.setDoInput(true);
		conn.setUseCaches(false);
		conn.setAllowUserInteraction(false);
		conn.setRequestProperty("Content-Type",
		"application/atom+xml");

		if (conn.getResponseCode() != 200) {
			throw new IOException(conn.getResponseMessage());
		}

		SAXBuilder builder = new SAXBuilder();
		Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		return builder.build(in);
	}
	
	public static void putToEXist(String urlStr, Document dom) throws Exception{
		URL url = new URL(urlStr);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("PUT");
		conn.setDoOutput(true);
		conn.setDoInput(true);
		conn.setUseCaches(false);
		conn.setAllowUserInteraction(false);
		conn.setRequestProperty("Content-Type",
		"application/atom+xml");

		// Create the form content
		OutputStream out = conn.getOutputStream();
		Writer writer = new OutputStreamWriter(out, "UTF-8");
		
		XMLOutputter xmlout = new XMLOutputter();
        xmlout.output(dom, writer);
		
		writer.flush();
		writer.close();
		out.close();

		int responseCode = conn.getResponseCode();
		if (responseCode != 204) {
			throw new IOException(conn.getResponseMessage() + " Code: " + responseCode);
		}
	
	}
	
	
	public static void postToEXist(String urlStr, Document dom) throws Exception {
		URL url = new URL(urlStr);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setRequestMethod("POST");
		conn.setDoOutput(true);
		conn.setDoInput(true);
		conn.setUseCaches(false);
		conn.setAllowUserInteraction(false);
		conn.setRequestProperty("Content-Type",
		"application/atom+xml");

		// Create the form content
		OutputStream out = conn.getOutputStream();
		Writer writer = new OutputStreamWriter(out, "UTF-8");
		
		XMLOutputter xmlout = new XMLOutputter();
        xmlout.output(dom, writer);
		writer.flush();
		writer.close();
		out.close();

		int responseCode = conn.getResponseCode();
		if (responseCode != 204 && responseCode != 201) {
			throw new IOException(conn.getResponseMessage() + " Code: " + responseCode);
		}
	}

	public static Document getFeed(String feedName) {
		try {
			return getFromEXist("http://localhost:8080/exist/atom/content/FoodAndWine/" + feedName);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
